package com.zlsy.es.dianping.service.impl;

import com.zlsy.es.dianping.baseexception.BizException;
import com.zlsy.es.dianping.constan.ErrorStatusEnum;
import com.zlsy.es.dianping.mapper.SellerMapper;
import com.zlsy.es.dianping.pojo.model.Seller;
import com.zlsy.es.dianping.service.SellerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author zhouliang
 * @date 2020/3/1 14:57
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SellerServiceImpl implements SellerService {

    private final SellerMapper sellerMapper;

    @Override
    public Seller create(Seller sellerModel) {
        sellerModel.setCreatedAt(new Date());
        sellerModel.setUpdatedAt(new Date());
        sellerModel.setRemarkScore(new BigDecimal(0));
        sellerModel.setDisabledFlag(0);

        sellerMapper.insertSelective(sellerModel);

        return get(sellerModel.getId());
    }

    @Override
    public Seller get(Integer id) {
        return sellerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Seller> selectAll() {
        return sellerMapper.selectAll();
    }

    @Override
    public Seller changeStatus(Integer id, Integer disabledFlag) {
        Seller seller = get(id);
        if (Objects.isNull(seller)) {
            throw new BizException(ErrorStatusEnum.PARAMETER_VALIDATION_ERROR);
        }
        seller.setDisabledFlag(disabledFlag);
        sellerMapper.updateByPrimaryKeySelective(seller);
        return seller;
    }

    @Override
    public Integer countAllSeller() {
        return sellerMapper.countAllSeller();
    }
}
